#ifndef UTIL_H
#define UTIL_H

#include "tux.h"
#include <wx/platinfo.h>
#include <wx/stdpaths.h>
#include <wx/filename.h>

#define WS2C(s)   (char*)(const char*)(s.mb_str(wxConvUTF8))
#define WS2UC(s)   (unsigned char*)(const char*)(s.mb_str(wxConvUTF8))
#define WS2CC(s)    (const char*)(s.mb_str(wxConvUTF8))
#define WSLEN(s)    strlen(s.mb_str(wxConvUTF8))

class StringUtil
{
public:
    static wxString md5(wxString& src,unsigned char* buf);

    static wxString ToHex(unsigned char c);
    
    static wxString ToHex(wxString& str);

	static wxString ToHex(unsigned char* buf,int len);

	static wxString ToHexFormat(unsigned char* buf, int len);

    static long ToLong(const wxString& value, int base=10);
	static wxString ToString(int value);

	static wxString SubString(const wxString& src, const wxString& start, const wxString& end);

	static void FromUTF16(char* src, int len, wxString& dest);
	static void ToUTF16(wxString& src, char* dest);

	static unsigned int ReadInt(char* buf, int offset, int len, bool littleEndian = true);
	static unsigned long long ReadInt8(char* buf, int offset, int len);

	static void WriteInt(char* buf, int offset, unsigned int value, bool littleEndian = true);
	static void WriteInt8(char* buf, int offset, unsigned long long value);

	static int IndexOf(char* buf, int bufSize, char* target, int targetSize);

    static char Base64_Value(char ch);
    static int Base64_Decode(char *text, char* buf, int size);
    static int Base64_Encode(char *text, char* buf, int size);

	static int base64encode(unsigned char *input, int input_length, unsigned char *output);

	static void Base64_Encode(const wxString& src, wxString& dest);
	static void Base64_Decode(const wxString& src, wxString& dest);

	static int URLDecode(char* src,char* dest,int len);
    static int URLEncode(char* src,char* dest,int len);
    
	static void URLEncode(const wxString& src, wxString& dest);
	static void URLDecode(const wxString& src, wxString& dest);

	static wxString URLDecode(const wxString& src);
    static wxString URLEncode(const wxString& src);

};

class NumberUtil
{
public:
	static int RandInt();

	static int RandInt(int min, int max);

	static int B2KB(int bytes);

};


class FileUtil
{
public:
    static bool MkDir(const wxString& path);

	static void OpenFileWIthDefaultApp(const wxString& path);

	static bool DeleteFile(const wxString& path);
};


/**
wxStandardPaths

wxSystemSettings

::wxGetMouseState
::wxGetMousePosition
**/

class SystemUtil
{
public:
    static wxString GetUserDataDir(){
        //return wxStandardPaths::Get().GetDataDir();
        return wxStandardPaths::Get().GetUserDataDir();
    }

    static wxString GetDataDir(){
        return wxStandardPaths::Get().GetDataDir();
    }

    static wxString GetAppPath(){
        return wxStandardPaths::Get().GetExecutablePath();
    }

    static wxString GetLocalAddress();

	static wxString GetOSName(){
		return ::wxGetOsDescription();
	}

	static wxColor GetDefaultBackgroundColor(){
		return wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
	}

	static wxString GetOSVersion(){
		int major,minor;
		::wxGetOsVersion(&major,&minor);
		wxString version;
		version<<major<<_T(".")<<minor;
		return version;
	}

	static wxString GetOSArch(){
		return wxPlatformInfo::Get().GetArchName();
	}

	static int GetIdleTime();

};

class SecurityUtil
{
public:
	static void Sha1(const wxFileName& fileNmae, unsigned char* digestBuf);

	

};


class MsnUtil
{
public:
	static wxString CreateMsnObject(const wxFileName& fileName, const wxString& passport, int fileType);

	static wxString Sha1D(const wxFileName& fileName);

};


class HttpUtil
{
protected:
    static size_t processHeader(void *ptr, size_t size, size_t nmemb, void *stream);

	static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream);
public:
    static wxString GetTicket(const wxString& info, const wxString& account, const wxString& password);

	static wxString TestPost(const wxString& cmd);

};

#endif

